package com.xx.sg.sms.impl;

import com.alibaba.fastjson2.JSONObject;
import com.xx.sg.sms.SmsService;
import com.xx.sg.util.AESSms;
import com.xx.sg.util.GZIPUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class SmsServiceImpl implements SmsService {

    @Value("${sms.app-id}")
    private String APP_ID;
    @Value("${sms.secret}")
    private String SECRET;
    @Value("${sms.http-address}")
    private String HTTP_ADDRESS;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public String sendMsg(List<String> tels) throws IOException {

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobiles", tels);
        jsonObject.put("content", "【内蒙古森工集团】 测试 ! ");
//        jsonObject.put("extendedCode", 112);
        jsonObject.put("requestTime", System.currentTimeMillis());
        jsonObject.put("requestValidPeriod", 30);

        byte[] data = jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8);
        data = GZIPUtils.compress(data);
        data = AESSms.encrypt(data, SECRET.getBytes(), AESSms.ALGORITHM_AEPP);

        // 请求头设置
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("appId", APP_ID);
        headers.set("gzip", "on");

        String url = HTTP_ADDRESS + "/inter/sendBatchOnlySMS";

        // 构造请求体
        HttpEntity<byte[]> requestEntity = new HttpEntity<>(data, headers);
        // 发送 POST 请求
        ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestEntity, byte[].class);

        byte[] resData = AESSms.decrypt(response.getBody(), SECRET.getBytes(), AESSms.ALGORITHM_AEPP);
        resData = GZIPUtils.decompress(resData);
        return new String(resData, StandardCharsets.UTF_8);
    }

    @Override
    public String sendMsg(List<String> tels, String content) throws IOException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobiles", tels);
        jsonObject.put("content", "【内蒙古森工集团】" + content);
//        jsonObject.put("extendedCode", 112);
        jsonObject.put("requestTime", System.currentTimeMillis());
        jsonObject.put("requestValidPeriod", 30);

        byte[] data = jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8);
        data = GZIPUtils.compress(data);
        data = AESSms.encrypt(data, SECRET.getBytes(), AESSms.ALGORITHM_AEPP);

        // 请求头设置
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("appId", APP_ID);
        headers.set("gzip", "on");

        String url = HTTP_ADDRESS + "/inter/sendBatchOnlySMS";

        // 构造请求体
        HttpEntity<byte[]> requestEntity = new HttpEntity<>(data, headers);
        // 发送 POST 请求
        ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestEntity, byte[].class);

        byte[] resData = AESSms.decrypt(response.getBody(), SECRET.getBytes(), AESSms.ALGORITHM_AEPP);
        resData = GZIPUtils.decompress(resData);
        return new String(resData, StandardCharsets.UTF_8);
    }
}
